﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlayListSet.Entity
{
    public delegate void PlayTimeChanged(object sender, double playTime);

    public abstract class PlayInfoBase : System.ComponentModel.INotifyPropertyChanged
    {
        private static Random random;
        static PlayInfoBase()
        {
            random = new Random();
        }
        public PlayInfoBase()
        {
            id = DateTime.Now.ToFileTimeUtc() + random.Next(1000);
          
        }
        public void UpdateId()
        {
            id = DateTime.Now.ToFileTimeUtc() + random.Next(1000);

        }
        private long id;
        [SqlSugar.SugarColumn(IsPrimaryKey = true)]
        public long ID
        {
            set { id = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ID")); }
            get { return id; }
        }
        private string name = "";
        public string Name
        {
            set { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); }
            get { return name; }
        }
        private string channel = "";
        public string Channel
        {
            set
            {
                channel = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Channel"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ChannelInfoStr"));
            }
            get { return channel; }
        }
        private int channelType =0;
        public int ChannelType
        {
            set { channelType = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ChannelType")); }
            get { return channelType; }
        }
        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public TimeSpan StartTimeCodeTimeSpan
        {
            set
            {

                if (value == null)
                {
                    StartTimeCode = 0;
                }
                else
                {
                    StartTimeCode = value.TotalSeconds;
                    //EndTimeCodeTimeSpan =  TimeSpan.FromMilliseconds(StartTimeCode+PlayTime);
                }
            }
            get
            {
                return TimeSpan.FromSeconds(StartTimeCode);
            }
        }
        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public TimeSpan EndTimeCodeTimeSpan
        {
            set
            {
                if (value == null)
                {
                    EndTimeCode = 0;
                }
                else
                {
                    EndTimeCode = value.TotalSeconds;

                }
            }
            get
            {
                return TimeSpan.FromSeconds(EndTimeCode);
            }
        }

        private double startTimeCode = 0;
        public double StartTimeCode
        {
            set { startTimeCode = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("StartTimeCodeTimeSpan"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("StartTimeCode"));
                StartTimeCodeChanged?.BeginInvoke(this, startTimeCode, null, null);
            }
            get { return startTimeCode; }
        }
        private double endTimeCode = 0;
        public double EndTimeCode
        {
            set
            {
                endTimeCode = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("EndTimeCode"));
                PropertyChanged?.BeginInvoke(this, new PropertyChangedEventArgs("PlayTime"), null, null);
                PlayTimeChanged?.BeginInvoke(this, PlayTime, null, null);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("EndTimeCodeTimeSpan"));

            }
            get { return endTimeCode; }
        }
        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public double PlayTime
        {
            get { return endTimeCode - StartTimeCode; }
        }
        #region UI Model
        public event PlayTimeChanged PlayTimeChanged;
        public event PlayTimeChanged StartTimeCodeChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        private double opacity = 1;
        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public double Opacity
        {
            set { opacity = value; PropertyChangedEvent("Opacity"); }
            get { return opacity; }
        }
        public void PropertyChangedEvent(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public void PropertyChangedEventAsync(string propertyName, AsyncCallback asyncCallback,object obj)
        {
            PropertyChanged?.BeginInvoke(this, new PropertyChangedEventArgs(propertyName), asyncCallback,obj);
        }
        #endregion
    }
}
